package packetsender;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {

	//*************************************************************MD5************************************************************************//
	public static String MD5(String text){
		StringBuffer md5=new StringBuffer(); 
		try{
			MessageDigest md=MessageDigest.getInstance("MD5");   
			md.update(text.getBytes());   
			byte[] digest=md.digest();          
			for(int i=0;i<digest.length;i++){
				md5.append(Character.forDigit((digest[i]&0xF0)>>4,16));   
				md5.append(Character.forDigit((digest[i]&0xF),16));   
			}   
		}catch(Exception e){
			e.printStackTrace();
		}
		return md5.toString();
	}
	//*************************************************************MD5************************************************************************//

	public static int byteToInt(byte b1, byte b2, byte b3, byte b4){
		int intValue = 0;

		intValue |= (b1 & 0xff)<<24;
		intValue |= (b2 & 0xff)<<16;
		intValue |= (b3 & 0xff)<<8;
		intValue |= (b4 & 0xff);

		return intValue;
	}

	public static int byteToLong(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8 ){
		int intValue = 0;

		intValue |= (b1 & 0xff)<<24;
		intValue |= (b2 & 0xff)<<16;
		intValue |= (b3 & 0xff)<<8;
		intValue |= (b4 & 0xff);

		return intValue;
	}

	public static short byteToShort(byte b1, byte b2){
		short value = 0;

		value |= (b1&0xff)<<8;
		value |= (b2&0xff);

		return value;
	}


	public static String setStringLength(String str, int length){
		byte[] read = new byte[length];
		for(int i=0; i<read.length; i++){
			read[i] = 32;
		}

		byte[] temp = str.getBytes();

		if(temp.length > length){
			for(int i = 0; i < length; i++){
				read[i] = temp[i];
			}
			//;if(GameDefine.PrintSession==1)System.out.println("重置字符串长度超限,原串：" + str + ",修改串：" + new String(read));
		}else{
			System.arraycopy(temp, 0, read, 0, temp.length);
		}
		return new String(read);
	}

	public static String getDateTimeByMillisecond(String str) {

		Date date = new Date(Long.valueOf(str));

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String time = format.format(date);

		return time;
	}

	/**
	 * 将时间转换成字符串形式
	 * @param time
	 * @return
	 */
	public static String parseToString(long time) {

		Date date = new Date(time);

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		return  format.format(date);
	}
}
